package com.example.administrator.demo.recycle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.administrator.demo.R;

import java.util.List;

/**
 */
public abstract class RecyclerViewRefreshAdapter<T> extends BaseRecyclerAdapter<RecyclerViewRefreshAdapter.CommonViewHolder> {

    protected Context mContext;
    protected LayoutInflater mInflater;
    protected int mLayoutId;
    protected List<T> mArrayList;
    protected OnItemClickListener<T> onItemClickListener;
    private final static int UPDATE_COUNT_DOWN_TIME = 1;

    public RecyclerViewRefreshAdapter(Context context, int layoutId, List<T> list) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mLayoutId = layoutId;
        this.mArrayList = list;
    }

    @Override
    public void onBindViewHolder(RecyclerViewRefreshAdapter.CommonViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if(position == mArrayList.size()) return;
        if (null == payloads || payloads.size() < 1) {
            onBindViewHolder(holder, position, true);
        } else{
            for (Object payload : payloads) {
                // 刷新倒计时
                if (payload instanceof Integer && ((int) payload) == UPDATE_COUNT_DOWN_TIME) {
                    updateClickItem(holder, position);
                }
            }
        }
    }

    @Override
    public void onBindViewHolder(RecyclerViewRefreshAdapter.CommonViewHolder holder, int position, boolean isItem) {
        final T bean = mArrayList.get(position);
        final int startPosition = position;
        holder.getView(R.id.recyclerItemView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null) {
                    notifyItemRangeChanged(startPosition, 1, UPDATE_COUNT_DOWN_TIME);
                    onItemClickListener.onClick(bean);
                }
            }
        });
        convert(holder, bean, position);
    }

    public abstract void convert(CommonViewHolder holder, T bean, int position);

    public void updateClickItem(CommonViewHolder holder, int position){}


    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return mArrayList.size();
    }

    @Override
    public CommonViewHolder getViewHolder(View view) {
        return new CommonViewHolder(view, false);
    }

    public void setData(List<T> list) {
        this.mArrayList = list;
        notifyDataSetChanged();
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = mInflater.inflate(mLayoutId, parent, false);
        CommonViewHolder vh = new CommonViewHolder(v, true);
        return vh;
    }

    public void insert(T bean, int position) {
        insert(mArrayList, bean, position);
    }

    public void remove(int position) {
        remove(mArrayList, position);
    }

    public void clear() {
        clear(mArrayList);
    }

    public T getItem(int position) {
        if (position < mArrayList.size())
            return mArrayList.get(position);
        else
            return null;
    }

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**********************************************************************************************/
    public interface OnItemClickListener<B>{
        void onClick(B bean);
    }


    /**********************************************************************************************/
    public class CommonViewHolder extends RecyclerView.ViewHolder {

        private SparseArray<View> mListView;
        private View mConvertView;

        public CommonViewHolder(View itemView, boolean isItem){
            super(itemView);
            if(isItem) {
                this.mConvertView = itemView;
                mListView = new SparseArray<>();
            }
        }

        /**
         * 通过viewId获取控件
         */
        public <T extends View> T getView(int viewId)
        {
            View view = mListView.get(viewId);
            if (view == null) {
                view = mConvertView.findViewById(viewId);
                mListView.put(viewId, view);
            }
            return (T) view;
        }
    }
}